package com.tongda.menu.service;


import java.util.List;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

public class ClearMemoryService extends Service{

	private final IBinder binder = new ClearMemoryBinder();
	
	public class ClearMemoryBinder extends Binder{
		public ClearMemoryService getService(){
			return ClearMemoryService.this;
		}
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
	}
	
	
	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}
	
	
	public void clearMemory(){
		ActivityManager activityManger=(ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> list=activityManger.getRunningAppProcesses();
        if(list!=null)
        for(int i=0;i<list.size();i++)
        {
            ActivityManager.RunningAppProcessInfo apinfo=list.get(i);
            
            System.out.println("pid            "+apinfo.pid);
            System.out.println("processName              "+apinfo.processName);
            System.out.println("importance            "+apinfo.importance);
            String[] pkgList=apinfo.pkgList;
            
            if(apinfo.importance>ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE)
            {
               // Process.killProcess(apinfo.pid);
                for(int j=0;j<pkgList.length;j++)
                {
 //                   activityManger.restartPackage(pkgList[j]);
                    //2.2以上适用,请用killBackgroundProcesses代替
                    activityManger.killBackgroundProcesses(pkgList[j]);
                } 
            }
        }
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
}
